package modul.data.login;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import model.util.conn.DataBaseConnection;

public class FormLogin extends javax.swing.JFrame {

    public FormLogin() {
        this.initComponents();
        this.setLocationRelativeTo(this);
        this.setIconImage(new ImageIcon(getClass().getResource("/modul/util/image/vcd.png")).getImage());
        this.setTitle("Login");
        this.timer = new Timer(20, listener);
    }

    private Timestamp time() {
        java.util.Date date = new java.util.Date();
        long time = date.getTime();
        Timestamp timeStemp = new Timestamp(time);
        return timeStemp;
    }

    private int noUrut() throws SQLException {
        Connection con = DataBaseConnection.getConnection();
        String query = "Select * from aktifitasoperator;";
        Statement statement = con.createStatement();
        ResultSet result = statement.executeQuery(query);
        int record = 0;
        while (result.next()) {
            record = result.getRow();
        }
        if (record == 0) {
            return 1;
        } else {
            return record+1;
        }
    }

    private void simpanAktifitasOperator() throws SQLException {
        String sql = "insert into aktifitasoperator (`NO`,ID_Operator,WaktuLogin) values"
                + "(?,?,?)";
        PreparedStatement st = DataBaseConnection.getConnection().prepareStatement(sql);
        st.setInt(1, noUrut());
        st.setString(2, jTextField1.getText());
        st.setTimestamp(3, time());
        st.executeUpdate();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jProgressBar1 = new javax.swing.JProgressBar();
        jCheckBox1 = new javax.swing.JCheckBox();
        buttonKeluar = new javax.swing.JButton();
        buttonLogin = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(204, 255, 204));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/modul/util/image/User_male.png"))); // NOI18N

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/modul/util/image/Key.png"))); // NOI18N

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/modul/util/image/Lock.png"))); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 64, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                .addGap(36, 36, 36))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        jLabel2.setText("ID");

        jLabel3.setText("Password");

        jPasswordField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jPasswordField1KeyPressed(evt);
            }
        });

        jCheckBox1.setText("As Owner");

        buttonKeluar.setText("Keluar");
        buttonKeluar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonKeluarActionPerformed(evt);
            }
        });

        buttonLogin.setText("Login");
        buttonLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonLoginActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(buttonLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
                        .addComponent(buttonKeluar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(23, 23, 23)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                            .addComponent(jCheckBox1)))
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBox1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonKeluar)
                    .addComponent(buttonLogin))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void buttonLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonLoginActionPerformed
    if ((jTextField1.getText()).equals("") || new String(jPasswordField1.getPassword()).equals("")) {
        JOptionPane.showMessageDialog(null, "ID and Password kosong, masukan ID dan Password dengan benar");
    } else {
        if (jCheckBox1.isSelected()) {
            try {
                String getId = "";
                String getPassword = "";
                String query = "Select Admin,password from owner where "
                        + "Admin='" + jTextField1.getText() + "' and password='" + new String(jPasswordField1.getPassword()) + "';";
                Statement statement = DataBaseConnection.getConnection().createStatement();
                ResultSet result = statement.executeQuery(query);
                int baris = 0;
                while (result.next()) {
                    baris = result.getRow();
                    getId = result.getString("Admin");
                    getPassword = result.getString("password");
                }
                if (baris == 1) {
                    if (jTextField1.getText().equals(getId)) {
                        if (new String(jPasswordField1.getPassword()).equals(getPassword)) {
                            JOptionPane.showMessageDialog(null, "Anda berhasil Login.");
                            timer.start();
                        } else {
                            JOptionPane.showMessageDialog(null, "Gagal Login, masukan ID dan Password dengan benar.");
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Gagal Login, masukan ID dan Password dengan benar.");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Gagal Login, masukan ID dan Password dengan benar.");
                }
            } catch (SQLException ex) {
            }
        } else {
            try {
                String getId = "";
                String getPassword = "";
                String query = "Select ID_Operator,Password from operator where "
                        + "ID_Operator='" + jTextField1.getText() + "' and Password='" + new String(jPasswordField1.getPassword()) + "';";
                Statement statement = DataBaseConnection.getConnection().createStatement();
                ResultSet result = statement.executeQuery(query);
                int baris = 0;
                while (result.next()) {
                    baris = result.getRow();
                    getId = result.getString("ID_Operator");
                    getPassword = result.getString("Password");
                }
                if (baris == 1) {
                    if (jTextField1.getText().equals(getId)) {
                        if (new String(jPasswordField1.getPassword()).equals(getPassword)) {
                            JOptionPane.showMessageDialog(null, "Anda berhasil Login.");
                            timer.start();
                            simpanAktifitasOperator();
                        } else {
                            JOptionPane.showMessageDialog(null, "Gagal Login, masukan ID dan Password dengan benar.");
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Gagal Login, masukan ID dan Password dengan benar.");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Gagal Login, masukan ID dan Password dengan benar.");
                }
            } catch (SQLException ex) {
            }
        }
    }
}//GEN-LAST:event_buttonLoginActionPerformed

private void jPasswordField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPasswordField1KeyPressed
    if (evt.getKeyCode() == 10) {
        buttonLoginActionPerformed(null);
    } else if (evt.getKeyCode() == 27) {
        buttonLoginActionPerformed(null);
    }
}//GEN-LAST:event_jPasswordField1KeyPressed

private void buttonKeluarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonKeluarActionPerformed
    if (JOptionPane.showConfirmDialog(null, "Apakah anda ingin keluar ?", "Pertanyaan", JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
        System.exit(0);
    }
}//GEN-LAST:event_buttonKeluarActionPerformed
    ActionListener listener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent event) {
            jCheckBox1.setEnabled(false);
            jProgressBar1.setValue(jProgressBar1.getValue() + 1);
            if (jCheckBox1.isSelected()) {
                if (jProgressBar1.getValue() == jProgressBar1.getMaximum()) {
                    timer.stop();
                    dispose();
                    OwnerHome jFH = new OwnerHome();
                    jFH.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
                    jFH.setVisible(true);
                }
            } else {
                if (jProgressBar1.getValue() == jProgressBar1.getMaximum()) {
                    timer.stop();
                    dispose();
                    OperatorHome jFH = new OperatorHome(jTextField1.getText());
                    jFH.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
                    jFH.setVisible(true);
                }
            }
        }
    };
    private Timer timer;
    private SimpleDateFormat spdt;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonKeluar;
    private javax.swing.JButton buttonLogin;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
